﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 提交管理平台V1
{
    public partial class frmPermissionConfig : Form
    {
        SqlTools sqt;

        string strSelectString = "select 权限 from 角色权限表 where 角色名称='@角色名称' order by 权限";
        string strInsertString = "insert into 角色权限表 (角色名称,权限) values ('@角色名称','@权限')";
        string strDeleteString = "delete from 角色权限表 where 角色名称='@角色名称'";

        public frmPermissionConfig()
        {
            InitializeComponent();
        }

        private void iniTreeView()
        {
            tvPermissions.CheckBoxes = true;
            tvPermissions.Nodes.Clear();
            //设置
            TreeNode root = new TreeNode("设置");
            root.Nodes.Add("字段设置");
            root.Nodes.Add("包装数量设置");
            root.Nodes.Add("打印机设置");
            root.Nodes.Add("用户管理");
            root.Nodes.Add("权限设置");
            root.Nodes.Add("修改密码");
            tvPermissions.Nodes.Add(root);
            //数据
            root = new TreeNode("数据");
            root.Nodes.Add("新增");
            root.Nodes.Add("删除");
            root.Nodes.Add("编辑");
            root.Nodes.Add("保存");
            root.Nodes.Add("取消");
            root.Nodes.Add("已开票");
            root.Nodes.Add("未开票");
            root.Nodes.Add("已完成");
            root.Nodes.Add("未完成");
            tvPermissions.Nodes.Add(root);
            //打印
            root = new TreeNode("打印");
            root.Nodes.Add("标签");
            root.Nodes.Add("发票");
            root.Nodes.Add("清单");
            root.Nodes.Add("资料");
            root.Nodes.Add("导出为Excel");
            tvPermissions.Nodes.Add(root);
        }

        private void frmPermissionConfig_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
            iniTreeView();
            cmbCharactor.SelectedIndex = 0;
        }

        private void tvPermissions_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                foreach (TreeNode tn in e.Node.Nodes)
                {
                    tn.Checked = e.Node.Checked;
                }
            }
        }

        private void ReadPermission()
        {
            if (cmbCharactor.SelectedIndex >= 0)
            {
                foreach (TreeNode tmptn in tvPermissions.Nodes)
                {
                    SetNodeUnselected(tmptn);
                }
                DataTable tmpdt = sqt.GetData(strSelectString.Replace("@角色名称", cmbCharactor.Text));
                if (tmpdt.Rows.Count > 0)
                {
                    NodesInfo ni=new NodesInfo (tvPermissions );
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        try
                        {
                            string strPath = GeneralTools.GetString(tmpdt.Rows[i]["权限"], string.Empty);
                            IntPtr ip=ni.GetNodeHandle (strPath );
                            if (ip==null )
                            {
                                continue ;
                            }
                            TreeNode tn = TreeNode.FromHandle(tvPermissions, ip);
                            tn.Checked = true;
                        }
                        catch
                        {
                        }
                    }
                }
                for (int i = 0; i < tvPermissions.Nodes.Count; i++)
                {
                    bool bAllSelected = true ;
                    for (int k = 0; k < tvPermissions.Nodes[i].Nodes.Count; k++)
                    {
                        if (!tvPermissions.Nodes[i].Nodes[k].Checked)
                        {
                            bAllSelected = false;
                            break;
                        }
                    }
                    if (bAllSelected)
                    {
                        tvPermissions.Nodes[i].Checked = true;
                    }
                }
            }
        }

        private void SetNodeUnselected(TreeNode tn)
        {
            tn.Checked = false;
            if (tn.Nodes.Count > 0)
            {
                foreach (TreeNode tmptn in tn.Nodes)
                {
                    SetNodeUnselected(tmptn);
                }
            }
        }

        private void cmbCharactor_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReadPermission();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            sqt.DoCmdExecuteNonQuery(strDeleteString.Replace("@角色名称", cmbCharactor.Text));
            for (int i = 0; i < tvPermissions.Nodes.Count; i++)
            {
                for (int k = 0; k < tvPermissions.Nodes[i].Nodes.Count; k++)
                {
                    if (tvPermissions.Nodes[i].Nodes[k].Checked)
                    {
                        sqt.DoCmdExecuteNonQuery(strInsertString.Replace("@角色名称", cmbCharactor.Text)
                                                                                                .Replace("@权限", tvPermissions.Nodes[i].Nodes[k].FullPath));
                    }
                }
            }
            GeneralTools.ShowInfoMsg("保存完成！");
        }
    }
}